import scrapy
import time
from scrapy.mail import MailSender


class HomeSpider(scrapy.Spider):
    name = 'home'
    allowed_domains = ['news.realsil.com.cn']
    start_urls = ['http://news.realsil.com.cn/']

    def parse(self, response):
        print("start")
        menu1= response.xpath('//div[@id="tab_meal_1"]')
        menu2= response.xpath('//div[@id="tab_meal_2"]')
        message='<div><table cellspacing="0" style="width:791px;""><tbody><tr><td colspan="8" style="background-color:#FF0000;border-width:1px;border-style:solid;border-color:black;height:37px;text-align:center;vertical-align:middle;text-wrap-mode:nowrap;width:760px;"><span style="font-size:29px;"><span style="font-family:黑体,monospace;">一厂</span></span></td></tr></tbody></table></div>'
        message +=menu1[0].get();
        message +='<div><table cellspacing="0" style="width:763px;"><tbody><tr><td colspan="8" style="background-color:#FF0000;border-width:1px;border-style:solid;border-color:black;height:37px;text-align:center;vertical-align:middle;text-wrap-mode:nowrap;width:760px;"><span style="font-size:29px;"><span style="font-family:黑体,monospace;">二厂</span></span></td></tr></tbody></table></div>'
        message +=menu2[0].get();
        print("end")
        self.sendmail(u"餐单", message)	
        pass

    def sendmail(self, title, message):
         #await self.sendmail_qq(message);
         self.sendmail_126(message, title);
    def sendmail_qq(self, message):
        #mailer = MailSender.from_settings(settings)# 出错了，没找到原因
        mailer = MailSender(
            smtphost = "smtp.qq.com",  # 发送邮件的服务器
            mailfrom = "75258661@qq.com",   # 邮件发送者
            smtpuser = "75258661@qq.com",   # 用户名
            smtppass = "feacaspyckwtbhia",
            #smtppass = "wqncrsfvyopmcaia",  # 发送邮箱的密码不是你注册时的密码，而是授权码！！！切记！
            smtpport = 465,   # 端口号
            smtpssl =True,
        )
        body = message
        subject = u'bby 在雪球更新了双低可转债('+time.ctime()+u')'
        # 如果说发送的内容太过简单的话，很可能会被当做垃圾邮件给禁止发送.
        print("发送邮件完成")
        return mailer.send(to=['jason_wang@realsil.com.cn', 'standback@126.com', '694103990@qq.com'], subject = subject, body = body)

    def sendmail_126(self, message,title):
        mailer = MailSender(
            smtphost = "smtp.126.com",  # 发送邮件的服务器
            mailfrom = "standback@126.com",   # 邮件发送者
            smtpuser = "standback@126.com",   # 用户名
           # smtppass = "feacaspyckwtbhia",
            smtppass = "PNPb9A4U9VjWLzZc",  # 发送邮箱的密码不是你注册时的密码，而是授权码！！！切记！
            smtpport = 465,   # 端口号:smtp:25,  ssl:465
            smtpssl =True,
        )
        #PNPb9A4U9VjWLzZc  #126.mail
        body = message
        subject = title +u'('+time.ctime()+u')'
        # 如果说发送的内容太过简单的话，很可能会被当做垃圾邮件给禁止发送.
        print("发送邮件完成")
        return mailer.send(to=['jason_wang@realsil.com.cn', '75258661@qq.com' , '694103990@qq.com'], mimetype='text/HTML', subject = subject, body = body)
